﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PSSMS.View.SysManage
{
    /// <summary>
    /// ClientSM_xg.xaml 的交互逻辑
    /// </summary>
    public partial class ClientSM_xg : Window
    {
        /// <summary>
        /// 声明全局变量
        /// </summary>
        DataRowView DGVR;
        bool blinsert = false;//默认操作
        BLL.Clientsm.ClientsmClient myClient = new BLL.Clientsm.ClientsmClient();
        public ClientSM_xg(DataRowView data)
        {
            InitializeComponent();
            DGVR = data;//接收传递的选中行
        }
        /// <summary>
        /// 窗口加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 1、下拉框绑定
            //客户类型
            DataTable dt = myClient.SelectSrType().Tables[0];
            khlx.ItemsSource = dt.DefaultView;
            khlx.DisplayMemberPath = "customerType";
            khlx.SelectedValuePath = "customerTypeID";
            //所属地区
            DataTable ds = myClient.SelDisName().Tables[0];
            ssdq.ItemsSource = ds.DefaultView;
            ssdq.DisplayMemberPath = "disName";
            ssdq.SelectedValuePath = "disID";
            //所属业务员
            DataTable dd = myClient.SelectPmethod().Tables[0];
            ssywy.ItemsSource = dd.DefaultView;//显示数据
            ssywy.DisplayMemberPath = "employeeName";
            ssywy.SelectedValuePath = "employeeID";
            #endregion

            #region 2、回填数据
            khmc.Text = DGVR.Row["customerName"].ToString().Trim();
            khlx.SelectedValue = Convert.ToInt32(DGVR.Row["customerTypeID"]);//客户类型
            lxr.Text = DGVR.Row["contactPerson"].ToString();
            lxdh.Text = DGVR.Row["contactNumber"].ToString();
            ssdq.SelectedValue = Convert.ToInt32(DGVR.Row["disID"]);
            lxdz.Text = DGVR.Row["contactAddress"].ToString();
            //把字符串类型转换成布尔值类型
            if (DGVR.Row["defaultNo"].ToString() == "默认")
            {
                mrkh.IsChecked = true;
            }
            if (DGVR.Row["disabledNo"].ToString() == "禁用")
            {
                jy.IsChecked = true;
            }
            bz.Text = DGVR.Row["remark"].ToString();
            ssywy.SelectedValue = Convert.ToInt32(DGVR.Row["employeeID"]);
            #endregion
        }
        
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断必填项不能为空
                if (khlx.SelectedValue != null && ssdq.SelectedValue != null && ssywy.SelectedValue != null)
                {
                    //2、获取控件值
                    int intCustomerTypeID = Convert.ToInt32(khlx.SelectedValue);//客户类型
                    string strCustomerName = khmc.Text.Trim();//客户名称
                    string strContactPerson = lxr.Text.Trim();//联系人
                    string strContactNumber = lxdh.Text.Trim();//联系电话 
                    string strRemark = bz.Text.Trim();//备注
                    int intDisID = Convert.ToInt32(ssdq.SelectedValue);//所属地区
                    int intEmployeeID = Convert.ToInt32(ssywy.SelectedValue);//所属业务员
                    string strContactAddress = lxdz.Text.Trim();//联系地址
                    bool blDefaultNo = (bool)mrkh.IsChecked;//默认
                    bool blDisabledNo = (bool)jy.IsChecked;//启用、禁用
                    int intCustomerID = Convert.ToInt32(DGVR.Row["customerID"]);
                    //3、代用服务方法执行修改
                    int intCount = myClient.UpdateStaff(intCustomerTypeID, strCustomerName,
         strContactPerson, strContactNumber, intDisID, strContactAddress,
         intEmployeeID, strRemark, blDefaultNo, blDisabledNo,intCustomerID);
                    //4、根据返回结果做提示
                    if (intCount > 0)
                    {
                        MessageBoxResult dr = MessageBox.Show("成功修改【" + strCustomerName + "】客户信息！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
                        this.Close();
                    }
                    else if (intCount == -1)
                    {
                        MessageBox.Show("修改客户信息重复！", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Stop);
                    }
                    else
                    {
                        MessageBoxResult dr = MessageBox.Show("修改【" + strCustomerName + "】客户信息有误！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);//弹出确定对话框
                    }
                }
                else
                {
                    MessageBox.Show("请检查是否存在空数据。", "系统提示：", MessageBoxButton.OK, MessageBoxImage.Stop);
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("关闭窗口，无法保存数据，是否要关闭？", "系统提示：", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //关闭窗口
                this.Close();
            }
        }

    }
}
